
#include <My/Price Action.mqh>
#include <My/GraphicalPanel.mqh>

input int   bo = 30;
input int   re = 60;

input bool LanguageHan = true;

CGraphicalPanel panel;

PriceAction    pa(bo, re);


int OnInit()
  {
      panel.OnInit();
      return(INIT_SUCCEEDED);
  }

void OnDeinit(const int reason)
  {
      panel.Destroy(reason);
  }

void OnTick()
  {
      double ask = SymbolInfoDouble(Symbol(), SYMBOL_ASK);
      datetime dt = TimeCurrent();
      
      pa.Update(ask, dt);
      
      ENUM_TREND     trend  = pa.GetMainTrend();
      ENUM_COLUMN    column = pa.GetCurrColumn();

      STRUCT_CELL pivot_upward     =  pa.GetLastPivot(UPWARD_TREND);
      STRUCT_CELL pivot_downward   =  pa.GetLastPivot(DOWNWARD_TREND);
      STRUCT_CELL pivot_rally      =  pa.GetLastPivot(NATURAL_RALLY);
      STRUCT_CELL pivot_reaction   =  pa.GetLastPivot(NATURAL_REACTION);
      
      if(LanguageHan){
         panel.UpdateHan(
         trend, 
         column, 
         pivot_upward.price, 
         pivot_downward.price, 
         pivot_rally.price, 
         pivot_reaction.price
         );
      }else{
         panel.Update(
         trend, 
         column, 
         pivot_upward.price, 
         pivot_downward.price, 
         pivot_rally.price, 
         pivot_reaction.price
         );
      }
      

      if(trend == UPWARD_TREND)
      {
          ObjectDelete(0, "Trendline_UPWARD");
          ObjectDelete(0, "Trendline_DOWNWARD");
          ObjectDelete(0, "Trendline_REACTION");
          ObjectDelete(0, "Trendline_RALLY");
          ObjectDelete(0, "_UPWARD_UP_");
          ObjectDelete(0, "_UPWARD_DOWN_");
          
          // Get the current bar information
          STRUCT_CELL cell = pa.GetLastPivot(UPWARD_TREND);
          STRUCT_CELL cell2 = pa.GetLastPivot(NATURAL_REACTION);
          // Create a trendline     
          MyCreateObject("_UPWARD_UP_", cell.dt, cell.price + bo*Point(), clrRed, STYLE_DOT, 1);  
          MyCreateObject("Trendline_UPWARD", cell.dt, cell.price, clrRed, STYLE_SOLID, 2);
          MyCreateObject("_UPWARD_DOWN_", cell.dt, cell.price - bo*Point(), clrRed, STYLE_DOT, 1);
          
          MyCreateObject("Trendline_REACTION", cell2.dt, cell2.price, clrBlue, STYLE_SOLID, 2);  

      }
      else if(trend == DOWNWARD_TREND)
      {
      
          ObjectDelete(0, "Trendline_UPWARD");
          ObjectDelete(0, "Trendline_DOWNWARD");
          ObjectDelete(0, "Trendline_REACTION");
          ObjectDelete(0, "Trendline_RALLY");
          
          // Get the current bar information
          STRUCT_CELL cell = pa.GetLastPivot(DOWNWARD_TREND);      
          STRUCT_CELL cell2 = pa.GetLastPivot(NATURAL_RALLY);
          
          MyCreateObject("Trendline_DOWNWARD", cell.dt, cell.price, clrGreen, STYLE_SOLID, 2);
          MyCreateObject("Trendline_RALLY", cell2.dt, cell2.price, clrBlue, STYLE_SOLID, 2); 
      }

  }

void OnChartEvent(const int id,const long& lparam,const double& dparam,const string& sparam)
{
   panel.PanelChartEvent(id, lparam, dparam, sparam);
}

void MyCreateObject(
                     const string object_name, 
                     datetime dt, 
                     double price, 
                     long clr, 
                     long style,
                     long width)
{
    ObjectCreate(0, object_name, OBJ_TREND, 0, dt, price, dt+60*60, price);

    ObjectSetInteger(0, object_name, OBJPROP_RAY_RIGHT, true);
    ObjectSetInteger(0, object_name, OBJPROP_COLOR, clr);
    ObjectSetInteger(0, object_name, OBJPROP_STYLE, style);
    ObjectSetInteger(0, object_name, OBJPROP_WIDTH, width);
       
}